package com.sgb.kjwl.view.ui.fragment.manage;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.GridLayoutManager;

import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.Marker;
import com.androidkun.xtablayout.XTabLayout;
import com.sgb.alimaplibrary.MapMarkerTools;
import com.sgb.alimaplibrary.util.ToastUtil;
import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentNoTypeBinding;
import com.sgb.kjwl.di.manage.DaggerEnterpriseManageFragComponent;
import com.sgb.kjwl.di.manage.EnterpriseManageFragModule;
import com.sgb.kjwl.interfaces.RecyclerClick;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.TwoLevelChildForPopWindowEntity;
import com.sgb.kjwl.model.entity.TwoLevelForPopWindowEntity;
import com.sgb.kjwl.model.entity.manage.TheEnterpriseOfEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.utils.DialogFunction;
import com.sgb.kjwl.utils.DialogHelper;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.ManageEvent;
import com.sgb.link.event.MaterialEvent;
import com.sgb.kjwl.view.ui.activity.manage.ShopInfoForCompanyTradesActivity;
import com.sgb.kjwl.view.ui.activity.self.userInfo.VerificationCodeLoginActivity;
import com.sgb.kjwl.view.ui.adapter.manage.RecommendedEnterprisesAdapter;
import com.sgb.kjwl.view.widget.EnterprisePopWindow;
import com.sgb.kjwl.view.widget.MapInfoDialog6;
import com.sgb.kjwl.view.widget.MapInfoWindow6;
import com.sgb.kjwl.view.widget.ThreeLevelAreaWindow;
import com.sgb.kjwl.view.widget.TwoLevelEntityPopWindow;
import com.sgb.kjwl.viewmodel.manage.NoTypeFragModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.entity.BaseAreaEntity;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

/**
 * 企业大全-没有分类的fragment
 */
public class NoTypeFragment extends AppFragment {

    private FragmentNoTypeBinding binding;
    private TheEnterpriseOfEntity entity;
    @Inject
    NoTypeFragModel model;
    private RecommendedEnterprisesAdapter recommendedEnterprisesAdapter;
    private ThreeLevelAreaWindow theEnterpriseOfAreaPopWindow;//省市弹窗
    private EnterprisePopWindow moneyPopWindow;//注册资金
    private EnterprisePopWindow distancePopWindow;//距离弹窗
    private List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> provinceData = new ArrayList<>();//省数据
    private List<PickSelectEntity> cityData = new ArrayList<>();//市数据
    private List<PickSelectEntity> priceData = new ArrayList<>();//注册资金
    private List<PickSelectEntity> typeData2 = new ArrayList<>();//距离数据
    private List<PickSelectEntity> distanceData = new ArrayList<>();//距离数据
    private List<TheEnterpriseOfEntity> mapList = new ArrayList<>();//地图数据
    private Map<String, Object> map = new HashMap<>();

    private boolean isCreate = false;
    private boolean init = true;
    private boolean isVisible;
    private boolean isMap;

    //省
    private String areacode = "";
    //地区
    private String citysNos = "";
    //金额
    private String price;
    //距离
    private String dic;
    private String longitude;
    private String latitude;
    private String serviceType;
    public MapMarkerTools mapMarkerTools;
    private Marker markerLast;
    private int lastPosition;
    private MapView mMapView;

    private String name1 = "";
    private String name2 = "";
    private String name3 = "";
    private String mOneLevelNo = "";
    private String mTwoLevelNo = "";
    private String mMapTitle = "";
    private int mPosition;
    private MapInfoDialog6 mapInfoDialog6;
    private TwoLevelEntityPopWindow<TwoLevelForPopWindowEntity> typePopWindow;//分类弹窗
    private MutableLiveData<Integer> mPositionForSecond = new MutableLiveData<>();


    public static NoTypeFragment newInstance(Context context, List<PickSelectEntity> priceData, String longitude, String latitude, boolean isMap, String serviceType) {
        NoTypeFragment fragment = new NoTypeFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("priceData", (Serializable) priceData);
        bundle.putString("longitude", longitude);
        bundle.putString("latitude", latitude);
        bundle.putBoolean("isMap", isMap);
        bundle.putString("serviceType", serviceType);
        fragment.setArguments(bundle);
        return fragment;
    }

    public static NoTypeFragment newInstance(Context context, String longitude, String latitude, boolean isMap, String serviceType) {
        NoTypeFragment fragment = new NoTypeFragment();
        Bundle bundle = new Bundle();
        bundle.putString("longitude", longitude);
        bundle.putString("latitude", latitude);
        bundle.putBoolean("isMap", isMap);
        bundle.putString("serviceType", serviceType);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerEnterpriseManageFragComponent.builder()
                .appComponent(appComponent)
                .enterpriseManageFragModule(new EnterpriseManageFragModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isVisible = isVisibleToUser;
        if (!isCreate) {
            return;
        }
        if (isVisibleToUser) {
            if (init) {
                init = false;
                init();
            }
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().postSticky(this);
        isCreate = true;
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_no_type, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView(savedInstanceState);
        if (init && isVisible) {
            init = false;
            init();
        }
    }

    private void initView(Bundle savedInstanceState) {
        PickSelectEntity pickSelectEntity = new PickSelectEntity();
        pickSelectEntity.setProjectscale("不限");
        pickSelectEntity.setNo("");
        serviceType = (String) getArguments().getSerializable("serviceType");
        priceData = (List<PickSelectEntity>) getArguments().getSerializable("priceData");
        longitude = getArguments().getString("longitude");
        latitude = getArguments().getString("latitude");
        isMap = getArguments().getBoolean("isMap", false);
        binding.tvDistance.setOnClickListener(this);
        binding.tvArea.setOnClickListener(this);
        binding.tvType.setOnClickListener(this);
        binding.tvPersonNumber.setOnClickListener(this);
        if (isMap) {
            mapMarkerTools = new MapMarkerTools(getActivity(), getLifecycle());
            binding.mapView.setVisibility(View.VISIBLE);
            binding.recyclerView.setVisibility(View.GONE);
            mapMarkerTools = new MapMarkerTools(getActivity(), getLifecycle());
            initMap(savedInstanceState);
        } else {
            binding.recyclerView.setVisibility(View.VISIBLE);
            binding.mapView.setVisibility(View.GONE);
        }
        //列表
        if (recommendedEnterprisesAdapter == null) {
            recommendedEnterprisesAdapter = new RecommendedEnterprisesAdapter(getActivity(), null);
        }
        binding.recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        binding.recyclerView.setAdapter(recommendedEnterprisesAdapter);
        recommendedEnterprisesAdapter.setOnItemClick(new AdapterOnItemClick<TheEnterpriseOfEntity>() {
            @Override
            public void onItemClick(TheEnterpriseOfEntity entity, int position) {
                Intent intent = new Intent(getActivity(), ShopInfoForCompanyTradesActivity.class);
                if (entity.getSupplierId() != null) {
                    intent.putExtra("companyNo", entity.getSupplier());
                    intent.putExtra("id", entity.getSupplierId());
                    startActivity(intent);
                } else {
                    Toast.makeText(getActivity(), "店铺id为空", Toast.LENGTH_SHORT).show();
                }
            }
        });

        recommendedEnterprisesAdapter.setmRecyclerClick(new RecyclerClick<TheEnterpriseOfEntity>() {
            @Override
            public void followClick(TheEnterpriseOfEntity entity) {

                if (DialogHelper.checkLogin()) {
                    if (entity.getFollow()) {
                        DialogHelper.showDialog(getContext(), "提示", "确定取消关注该企业？", "取消关注", new DialogFunction() {
                            @Override
                            public void normalRoundDialog() {
                                model.follow(entity.getSupplier(), 0);
                            }
                        });
                    } else {
                        model.follow(entity.getSupplier(), 0);
                    }
                } else {
                    VerificationCodeLoginActivity.start(getContext(), EventConstant.EVENT_MANAGE);
                }
            }
        });

        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                model.getComList(false, map, 0);
            }

            @Override
            public void onLoadMore() {
                model.getComList(true, map, 0);
            }
        });

        model.getTradeTypes();
        typePopWindow = new TwoLevelEntityPopWindow<>(getActivity());
        typePopWindow.setBgView(binding.grayLayout);
        typePopWindow.setmTwoLevelClick(new TwoLevelEntityPopWindow.TwoLevelClick<TwoLevelForPopWindowEntity>() {
            @Override
            public void oneClick(TwoLevelChildForPopWindowEntity<List<TwoLevelForPopWindowEntity>> d, int position) {
                name3 = d.getName();
                if (!mOneLevelNo.equals(d.getId())) {
                    mTwoLevelNo = "";
                    map.put("industryCategory2", mTwoLevelNo);
                }

                mOneLevelNo = d.getId();
                map.put("industryCategory1", mOneLevelNo);
            }

            @Override
            public void twoClick(TwoLevelForPopWindowEntity twoLevelEntityForPopWindow, int position) {
                name3 = twoLevelEntityForPopWindow.getName();
                mTwoLevelNo = twoLevelEntityForPopWindow.getId();
                map.put("industryCategory2", mTwoLevelNo);
            }

            @Override
            public void reset() {
                typePopWindow.dismiss();
                model.getComList(false, map, 0);
            }
        });

        typePopWindow.setOnResultClick((key1, key2, key3) -> model.getComList(false, map, 0));
        typePopWindow.setOnDismissListener(() -> binding.tvType.contentSelect(!name3.equals("不限") && !name3.equals(""), false));

        // 省 城市
        theEnterpriseOfAreaPopWindow = new ThreeLevelAreaWindow(getActivity()) {
            @Override
            protected void provinceClick(BaseAreaEntity baseAreaEntity, int position) {
                super.provinceClick(baseAreaEntity, position);
                name1 = baseAreaEntity.getSimpleName();
                if (!areacode.equals(baseAreaEntity.getId())) {
                    citysNos = "";
                    map.put("areaNo", citysNos);
                }

                areacode = baseAreaEntity.getId();

                map.put("provinceNo", areacode);
            }

            @Override
            protected void cityClick(BaseAreaEntity baseAreaEntity, int position) {
                super.cityClick(baseAreaEntity, position);
                citysNos = baseAreaEntity.getId();

                name1 = baseAreaEntity.getSimpleName();
                map.put("areaNo", citysNos);
            }

            @Override
            protected void reset() {
                super.reset();
                theEnterpriseOfAreaPopWindow.dismiss();
                model.getComList(false, map, 0);
            }
        };
        theEnterpriseOfAreaPopWindow.setBgView(binding.grayLayout);
        theEnterpriseOfAreaPopWindow.setThreeListIsGone(true);
        theEnterpriseOfAreaPopWindow.setProvinceSingle();
        theEnterpriseOfAreaPopWindow.setCitySingle();
        theEnterpriseOfAreaPopWindow.setOnResultClick((key1, key2, key3) -> model.getComList(false, map, 0));
        theEnterpriseOfAreaPopWindow.setOnDismissListener(() -> {
            binding.tvArea.contentSelect(!name1.equals("") && (!name1.equals("不限")), false);
            if (areacode.equals("")) {
                binding.tvArea.contentSelect(false, false);
            }
        });

        // 注册资金
        moneyPopWindow = new EnterprisePopWindow(getActivity());
        moneyPopWindow.setBgView(binding.grayLayout);
        moneyPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                price = (String) key1;
                name2 = (String) key2;
                //请求接口
                map.put("staffNumber", price);
                model.getComList(false, map, 0);
            }
        });
        moneyPopWindow.setOnDisMissClick(() -> binding.tvPersonNumber.contentSelect(!name2.equals("") && !name2.equals("不限"), false));
        // 距离
        distancePopWindow = new EnterprisePopWindow(getActivity());
        distancePopWindow.setBgView(binding.grayLayout);
        distancePopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                try {
                    String distance = (String) key1;
                    if (!distance.equals("")) {
                        dic = Long.valueOf(distance) * 1000 + "";
                    } else {
                        dic = distance;
                    }
                    name3 = (String) key2;
                    //请求接口
                    map.put("distance", dic);
                    model.getComList(false, map, 0);
                } catch (Exception e) {
                    Toast.makeText(getActivity(), "您输入的数字太大", Toast.LENGTH_SHORT).show();
                }
            }
        });
        distancePopWindow.setOnDisMissClick(() -> binding.tvDistance.contentSelect(!name3.equals("") && !name3.equals("不限"), false));

        binding.tlCompanyTradesType.addOnTabSelectedListener(new XTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(XTabLayout.Tab tab) {
                String type = typeData2.get(tab.getPosition()).getNo();
                map.put("dictId", type);
                model.getComList(false, map, 0);
                mPositionForSecond.setValue(tab.getPosition());
            }

            @Override
            public void onTabUnselected(XTabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(XTabLayout.Tab tab) {

            }
        });

        model.getType();

    }

    private void showProvincePopWindow() {
        theEnterpriseOfAreaPopWindow.showPopwindow(binding.tvArea);
    }

    private void showDistancePopWindowPopWindow() {
        distancePopWindow.showPopwindow(binding.tvDistance);
        distancePopWindow.setData(distanceData, true, false);
    }

    public void setCityData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> datas, String areaCode) {
        provinceData = datas;
        theEnterpriseOfAreaPopWindow.setDataForCompany(datas, true);
    }

    //设置注册资金数据
    public void setMoneyDatas(List<PickSelectEntity> data) {
        moneyPopWindow.setData(data, false, false);
        moneyPopWindow.showPopwindow(binding.tvPersonNumber);
    }

    public void showType(List<PickSelectEntity> datas) {
//        typePopWindow.setData(datas, false, false);
//        typePopWindow.showPopwindow(binding.tvType);
        typeData2 = datas;
        for (PickSelectEntity data : datas) {
            binding.tlCompanyTradesType.addTab(binding.tlCompanyTradesType.newTab().setText(data.getPickerViewText()));
        }
    }

    //距离
    public void setDistanceDatas(List<PickSelectEntity> datas) {
        distanceData.clear();
        distanceData.addAll(datas);
        distancePopWindow.setData(distanceData, true, false);
        distancePopWindow.showPopwindow(binding.tvDistance);
    }

    /**
     * 设置列表数据
     *
     * @param list
     * @param isLoadMore
     */
    public void showListData(List<TheEnterpriseOfEntity> list, boolean isLoadMore, int type) {
        if (!isMap) {
            binding.recyclerView.refreshComplete();
            if (isLoadMore) {
                if (null != list && list.size() > 0) {
                    binding.recyclerView.loadMoreComplete();
                    recommendedEnterprisesAdapter.addDatas(list);
                } else {
                    binding.recyclerView.setNoMore(true);
                }
                binding.recyclerView.setVisibility(View.VISIBLE);
            } else {
                binding.recyclerView.refreshComplete();
                if (null != list && list.size() > 0) {
                    binding.recyclerView.setVisibility(View.VISIBLE);
                    binding.emptyView.setVisibility(View.GONE);
                    recommendedEnterprisesAdapter.setDatas(list);
                    binding.recyclerView.setLoadingMoreEnabled(true);
                } else {
                    recommendedEnterprisesAdapter.setDatas(null);
                    binding.recyclerView.setVisibility(View.GONE);
                    binding.emptyView.setVisibility(View.VISIBLE);
                }
            }
        } else {
            addMarkInfoWindow(list, type);
        }
    }

    private void addMarkInfoWindow(List<TheEnterpriseOfEntity> datas, int type) {
        try {
            mapMarkerTools.mapClear();
            mapList.clear();
            if (datas.size() == 0) {
                Toast.makeText(getActivity(), "暂无数据", Toast.LENGTH_SHORT).show();
                return;
            }
            for (int i = 0; i < datas.size(); i++) {
                datas.size();
                TheEnterpriseOfEntity entity = datas.get(i);
                if (null != entity && !TextUtils.isEmpty(entity.getLatitude()) && !TextUtils.isEmpty(entity.getLongitude())) {
                    double currentLat = Double.parseDouble(entity.getLatitude());
                    double currentLong = Double.parseDouble(entity.getLongitude());
                    if (currentLat != 0 && currentLong != 0) {
                        mapMarkerTools.addPoiItem(currentLat, currentLong, "");
                        mapList.add(datas.get(i));
                    }
                }
            }
            mapMarkerTools.addAllMarkerToMap(0, true);

            if (type == 1) {
                BitmapDescriptor bitmapSmall = BitmapDescriptorFactory.fromView(setMarkStyle(Integer.valueOf(lastPosition), markerLast, true));
                markerLast.setIcon(bitmapSmall);
                resetMarker(markerLast.getId(), markerLast, mPosition);
                mapMarkerTools.jumpPoint(markerLast);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void initMap(Bundle savedInstanceState) {
        mMapView = binding.mapView;
        mapMarkerTools.init(savedInstanceState, mMapView);
        mapMarkerTools.setMarkerClick(new MapMarkerTools.MarkerClick() {
            @Override
            public void onMarkerClick(int pos, String title) {
            }

            @Override
            public void onMarkerClick(int pos, String title, Marker marker) {
                mMapTitle = title;
                showMarkerView(pos, title);
                BitmapDescriptor bitmapSmall = BitmapDescriptorFactory.fromView(setMarkStyle(Integer.valueOf(title), marker, true));
                marker.setIcon(bitmapSmall);
                resetMarker(marker.getId(), marker, pos);
                mPosition = pos;
                markerLast = marker;
                lastPosition = Integer.valueOf(title);
                mapMarkerTools.jumpPoint(marker);
            }

            @Override
            public void onLocationChanged(Double currentLat, Double currentLong, String address) {

            }
        });
    }

    private View setMarkStyle(int pos, Marker marker, boolean isBigMarkView) {
        View markerView = LayoutInflater.from(getActivity()).inflate(com.sgb.alimaplibrary.R.layout.view_marker, null);
        ImageView imageView = markerView.findViewById(com.sgb.alimaplibrary.R.id.marker);
        TextView textView = markerView.findViewById(com.sgb.alimaplibrary.R.id.text);
        textView.setText(pos + 1 + "");
        Drawable drawable = getResources().getDrawable(isBigMarkView ? com.sgb.alimaplibrary.R.drawable.marker_big : com.sgb.alimaplibrary.R.drawable.marker);
        int smallFontSize = (int) getResources().getDimension(com.sgb.alimaplibrary.R.dimen.sp5);
        int bigFontSize = (int) getResources().getDimension(com.sgb.alimaplibrary.R.dimen.sp8);
        textView.setTextSize(isBigMarkView ? bigFontSize : smallFontSize);
        imageView.setImageDrawable(drawable);
        return markerView;
    }

    private void resetMarker(String curMarkerId, Marker markerNew, int pos) {
        if (null != markerLast) {
            if (!TextUtils.equals(curMarkerId, markerLast.getId())) {
                markerLast.setIcon(BitmapDescriptorFactory.fromView(setMarkStyle(lastPosition, markerLast, false)));
            }
        }

    }

    public boolean showMarkerView(int pos, String title) {
        entity = mapList.get(Integer.valueOf(title));
        MapInfoWindow6 mapInfoWindow = new MapInfoWindow6(getActivity());
        mapInfoWindow.setData(entity);
        mapInfoDialog6 = new MapInfoDialog6(getActivity(), null);
        mapInfoDialog6.getWindow().setDimAmount(0f);
        mapInfoDialog6.setDialogClickCallBack(new MapInfoDialog6.DialogClickCallBack() {
            @Override
            public void onClickShop(String shopId) {
                if (TextUtils.isEmpty(shopId)) {
                    ToastUtil.show(getContext(), "企业店铺id不能为空");
                    return;
                }
                ShopInfoForCompanyTradesActivity.startAct(getActivity(), shopId, "", 0, 0,entity.getFollow());
            }

            @Override
            public void onClickFollow(TheEnterpriseOfEntity entity) {
                if (DialogHelper.checkLogin()) {
                    if (entity.getFollow()) {
                        DialogHelper.showDialog(getContext(), "提示", "确定取消关注该企业？", "取消关注", new DialogFunction() {
                            @Override
                            public void normalRoundDialog() {
                                model.follow(entity.getSupplier(), 1);
                            }
                        });
                    } else {
                        model.follow(entity.getSupplier(), 1);
                    }
                } else {
                    VerificationCodeLoginActivity.start(getContext(), EventConstant.EVENT_MANAGE);
//                            DialogHelper.showLoginDialogNew(getContext(), EventConstant.EVENT_MANAGE, "温馨提示", "登录后可拨打电话,去登录", "登录");
                }
            }
        }).setData(mapInfoWindow.getData()).show();
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_area:
                binding.tvArea.contentSelect(false, true);
                showProvincePopWindow();
                break;
            case R.id.tv_person_number:
                binding.tvPersonNumber.contentSelect(false, true);
                model.findCapitalList();
                break;
            case R.id.tv_distance:
                binding.tvDistance.contentSelect(false, true);
                if (distanceData.size() == 0) {
                    model.getDistanceDatas();
                } else {
                    showDistancePopWindowPopWindow();
                }
                break;
            case R.id.tv_type:
                binding.tvType.contentSelect(false, true);
                showType();
                break;
        }
    }


    private void init() {
        map.put("longitude", longitude);
        map.put("latitude", latitude);
        map.put("serviceType", serviceType);
        if (isMap) {
            map.put("distance", "1000000000");
        } else {
            map.put("onMap", "0");
        }
        model.getCityData();
        model.getComList(false, map, 0);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void data(String text, String position) {
        map.put("name", text);
        map.put("serviceType", position);
        model.getComList(false, map, 0);
    }

    public void setType(List<TwoLevelChildForPopWindowEntity<List<TwoLevelForPopWindowEntity>>> data) {
        typePopWindow.setData(data);
    }

    private void showType() {
        typePopWindow.showPopwindow(binding.tvType, 0, 0);
    }

    public void followSuccess(int type) {
        if (type == 1) {
            mapInfoDialog6.updateText();

//            model.getComList(false, map,1);
            entity.setFollow(!(entity.getFollow()));
            mapList.get(Integer.parseInt(mMapTitle)).setFollow(!(mapList.get(Integer.parseInt(mMapTitle)).getFollow()));
            resetMarker(markerLast.getId(), markerLast, mPosition);
        } else {
            model.getComList(false, map, 0);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBus(MaterialEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_MANAGE) {
            model.getComList(false, map, 0);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!isMap) {
            model.getComList(false, map, 0);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e("onActivityResult: ", "hgasjkdghasjkdlasdjh");
        if (resultCode == 1) {
            if (data.getBooleanExtra("关注", false) != mapList.get(Integer.parseInt(mMapTitle)).getFollow()) {
                mapInfoDialog6.updateText();

                entity.setFollow(!(entity.getFollow()));
                mapList.get(Integer.parseInt(mMapTitle)).setFollow(!(mapList.get(Integer.parseInt(mMapTitle)).getFollow()));
                resetMarker(markerLast.getId(), markerLast, mPosition);
            }
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(ManageEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_MANAGE_MAP_FOLLOW) {
            mapInfoDialog6.updateText();

            entity.setFollow(!(entity.getFollow()));
            mapList.get(Integer.parseInt(mMapTitle)).setFollow(!(mapList.get(Integer.parseInt(mMapTitle)).getFollow()));
            resetMarker(markerLast.getId(), markerLast, mPosition);
        }
        EventBus.getDefault().removeStickyEvent(event);
    }


}
